/*
 * Button.cpp
 *
 *  Created on: 24-11-2010
 *      Author: Przemek
 */

#include "gui/Button.hpp"
#include "gui/UIManager.hpp"
#include "gui/ClipArea.hpp"
#include "utils/GLUtils.hpp"
using namespace std;

Button::Button(const string& text) :
	text(text), state(NORMAL)
{
}

Button::~Button()
{

}

void Button::setText(const string& text)
{
	this->text = text;
}

string Button::getText() const
{
	return text;
}

void Button::draw(const ClipArea& clipArea)
{
	clipArea.setGLClipArea();
	UIManager::getInstance().getButtonFrameColor().setGLState();
	GLUtils::drawRectangle(Vector(0, 0), getSize());

	setInsideButtonColor();
	GLUtils::drawRectangle(Vector(1, 1), Dimension(getSize().getWidth() - 2, getSize().getHeight()
			- 2));

	UIManager::getInstance().getButtonTextColor().setGLState();
	const GLFont& font = UIManager::getInstance().getGUIFont();
	font.begin();
	font.drawTextAt(text, getCenteredTextLeftCornerPosition(text));
	font.end();
}

void Button::setInsideButtonColor() const
{
	const float COLOR_CHANGE_AMOUNT = 0.2f;
	if(state == PRESSED)
	{
		UIManager::getInstance().getButtonInsideColor().darken(COLOR_CHANGE_AMOUNT).setGLState();
	}
	else if(state == HOVERED)
	{
		UIManager::getInstance().getButtonInsideColor().lighten(COLOR_CHANGE_AMOUNT).setGLState();
	}
	else
	{
		UIManager::getInstance().getButtonInsideColor().setGLState();
	}
}

void Button::mousePressed(const MouseListener::EventArguments& arguments)
{
	state = PRESSED;
	notifyMousePressed(arguments);
}

void Button::mouseEntered()
{
	state = HOVERED;
	notifyMouseEntered();
}

void Button::mouseExited()
{
	if(state == PRESSED)
	{
		mouseReleased(MouseListener::EventArguments());
	}
	state = NORMAL;
	notifyMouseExited();
}

void Button::mouseReleased(const MouseListener::EventArguments& arguments)
{
	state = NORMAL;
	notifyMouseReleased(arguments);
}
